//
//  KLBaseViewController.swift
//  新浪微博
//
//  Created by WTFKL on 17/8/4.
//  Copyright © 2017年 WTF. All rights reserved.
//

import UIKit


class KLBaseViewController: UIViewController {

    lazy var navigationBar = UINavigationBar(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 64));
    lazy var navItem = UINavigationItem();
    var tableV  : UITableView?;
    //访客视图字典
    var visitInfo : [String : String]?;

    lazy var header = MJRefreshNormalHeader();
    lazy var footer = MJRefreshBackNormalFooter();
    //判断是否登录,
    var iflogOn = false;
    
    override func viewDidLoad() {
        super.viewDidLoad()
        iflogOn ? setupUI() : setupVistorView();
        loadData();
    }
    /// 加载数据专用方法-待重写
    func loadData(){
        
    }
    
}
///登录注册方法
extension KLBaseViewController {
    func loginClick(){
        print("登录")
    }
    func registClick(){
        print("注册");
    }
}




extension KLBaseViewController {
    //初始化UI
   fileprivate func setupUI(){
        addNavItem();
        addTableView();
    }
    /// 初始化访客视图
   fileprivate func setupVistorView(){
        addNavItem();
        let vistView = KLVistorView(frame: view.bounds);
        view.insertSubview(vistView, belowSubview: navigationBar);
        
        //添加监听事件
        vistView.registBtn.addTarget(self, action: #selector(registClick), for: .touchUpInside);
        vistView.loginBtn.addTarget(self, action: #selector(loginClick), for: .touchUpInside);
        //添加导航条按钮
        navItem.leftBarButtonItem = UIBarButtonItem(title: "注册", style: .plain, target: self, action: #selector(registClick), normalTextColor: #colorLiteral(red: 0.9372549057, green: 0.3490196168, blue: 0.1921568662, alpha: 1), highLightTextColor: #colorLiteral(red: 0.9372549057, green: 0.3490196168, blue: 0.1921568662, alpha: 1));
        navItem.rightBarButtonItem = UIBarButtonItem(title: "登录", style: .plain, target: self, action: #selector(registClick), normalTextColor: #colorLiteral(red: 0.9372549057, green: 0.3490196168, blue: 0.1921568662, alpha: 1), highLightTextColor: #colorLiteral(red: 0.9372549057, green: 0.3490196168, blue: 0.1921568662, alpha: 1));
        vistView.visitDict = visitInfo;
    }
    /// 添加导航栏
   fileprivate func addNavItem(){
        navigationBar.items = [navItem];
        view.addSubview(navigationBar);
    }

    //添加tableview
    func addTableView(){
        tableV = UITableView(frame: view.bounds, style: .plain);
        
        //添加守护
        guard let tableV = tableV else { return;}
        
        tableV.contentInset = UIEdgeInsetsMake(44, 0, 0, 0);
        /// waring : 刷新bug
        header.setRefreshingTarget(self, refreshingAction:#selector(loadData));
        footer.setRefreshingTarget(self, refreshingAction: #selector(loadData));
        tableV.mj_header = header;
        tableV.mj_footer = footer;
        view.insertSubview(tableV, belowSubview: navigationBar);
        tableV.delegate = self;
        tableV.dataSource = self;
    }
    
    //重写set方法
    override var title: String? {
        didSet{
            navItem.title = title;
        }
    }
    
}

/// Mark  数据源
extension KLBaseViewController : UITableViewDelegate,UITableViewDataSource {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 0;
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        return UITableViewCell();
    }
    
}



























